package me.dz.dreamcleaner;

import com.pixelmonmod.pixelmon.entities.pixelmon.EntityPixelmon;
import io.lumine.mythic.bukkit.BukkitAPIHelper;
import io.lumine.mythic.bukkit.MythicBukkit;
import io.lumine.xikage.mythicmobs.MythicMobs;
import io.lumine.xikage.mythicmobs.mobs.MobManager;
import me.dz.dreamcleaner.bstats.Metrics;
import me.dz.dreamcleaner.command.ACommand;
import me.dz.dreamcleaner.command.Cmd;
import me.dz.dreamcleaner.listener.*;
import me.dz.dreamcleaner.manager.*;
import me.dz.dreamcleaner.runnable.ChatTask;
import me.dz.dreamcleaner.runnable.ClearTask;
import me.dz.dreamcleaner.runnable.LogTask;
import me.dz.dreamcleaner.utils.TrashUtils;
import me.dz.mod.dreamcleaner.entity.EntityManager;
import net.md_5.bungee.api.ChatMessageType;
import net.minecraft.entity.Entity;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.*;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;

import java.io.*;
import java.util.*;

import static me.dz.dreamcleaner.utils.MsgUtils.*;
import static me.dz.dreamcleaner.runnable.ClearTask.*;

public final class DreamCleaner extends JavaPlugin {

    public static DreamCleaner pl;
    public static String prefix = "&3DreamCleaner &f>>>";

    public static boolean itemEnable = false;
    public static boolean itemAsync = false;
    public static boolean itemBar = false;
    public static boolean itemAction = false;
    public static boolean itemMsg = false;
    public static boolean itemAlone = false;
    public static boolean itemIssue = false;
    public static boolean itemCheck = false;
    public static String itemBarMsg;
    public static String itemActionMsg;
    public static int itemBarTime;
    public static int itemActionTime;
    public static int itemTime;
    public static ArrayList<Integer> itemRemind;
    public static ArrayList<String> itemBlackWorld;
    public static ArrayList<String> itemType;
    public static ArrayList<String> itemName;
    public static ArrayList<String> itemLore;
    public static ArrayList<String> itemVagueType;
    public static ArrayList<String> itemVagueName;
    public static ArrayList<String> itemVagueLore;
    public static ArrayList<ItemType> itemCleaner;
    public static HashMap<String,String> itemClearMsg;

    public static boolean denseEnable = false;
    public static boolean denseAsync = false;
    public static boolean denseCheck = false;
    public static double denseScope;
    public static int denseAmount;
    public static int denseRemind;
    public static String denseMsg;
    public static ArrayList<String> denseBlackWorld;
    public static ArrayList<String> denseType;
    public static ArrayList<String> denseName;
    public static ArrayList<String> denseLore;
    public static ArrayList<String> denseVagueType;
    public static ArrayList<String> denseVagueName;
    public static ArrayList<String> denseVagueLore;
    public static ArrayList<ItemType> denseCleaner;

    public static boolean trashEnable = false;
    public static boolean trashItem = false;
    public static int trashReset;
    public static int trashSize;
    public static String trashPermission;
    public static String trashTitle;
    public static String trashOnName;
    public static String trashOnType;
    public static String trashDownName;
    public static String trashDownType;
    public static ArrayList<String> trashOnLore;
    public static ArrayList<String> trashDownLore;
    public static ArrayList<String> trashType;
    public static ArrayList<String> trashName;
    public static ArrayList<String> trashLore;
    public static ArrayList<String> trashVagueType;
    public static ArrayList<String> trashVagueName;
    public static ArrayList<String> trashVagueLore;
    public static HashMap<String,String> trashMsg;

    public static boolean gatherEnable = false;
    public static boolean gatherAsync = false;
    public static boolean gatherMythic = false;
    public static boolean gatherCheckName = false;
    public static boolean gatherTameable = false;
    public static boolean gatherCheck = false;
    public static double gatherScope;
    public static int gatherAmount;
    public static int gatherRemind;
    public static String gatherMsg;
    public static ArrayList<String> gatherBlackWorld;
    public static ArrayList<String> gatherName;
    public static ArrayList<String> gatherVagueName;
    public static ArrayList<String> gatherType;
    public static ArrayList<String> gatherVagueType;
    public static ArrayList<String> gatherEntity;
    public static ArrayList<String> gatherVagueEntity;
    public static ArrayList<MobsType> gatherCleaner;

    public static boolean mobsEnable = false;
    public static boolean mobsAsync = false;
    public static boolean mobsBar = false;
    public static boolean mobsAction = false;
    public static boolean mobsMsg = false;
    public static boolean mobsAlone = false;
    public static boolean mobsIssue = false;
    public static boolean mobsMeet = false;
    public static boolean mobsServer = false;
    public static boolean mobsMythic = false;
    public static boolean mobsTameable = false;
    public static boolean mobsCheckName = false;
    public static boolean mobsCheck = false;
    public static String mobsAmount;
    public static String mobsBarMsg;
    public static String mobsActionMsg;
    public static int mobsActionTime;
    public static int mobsTime;
    public static int mobsBarTime;
    public static ArrayList<Integer> mobsRemind;
    public static ArrayList<String> mobsBlackWorld;
    public static ArrayList<String> mobsType;
    public static ArrayList<String> mobsName;
    public static ArrayList<String> mobsVagueType;
    public static ArrayList<String> mobsVagueName;
    public static ArrayList<String> mobsEntity;
    public static ArrayList<String> mobsVagueEntity;
    public static ArrayList<MobsType> mobsCleaner;
    public static HashMap<String,String> mobsClearMsg;

    public static boolean pokeEnable = false;
    public static boolean pokeShiny = false;
    public static ArrayList<PokeType> pokeTypes = new ArrayList<>();
    public static ArrayList<String> pokeClear = new ArrayList<>();
    public static long pokeLike;

    public static HashMap<String,String> alias;

    public static boolean cacheEnable = false;
    public static int cacheReset;

    public static boolean repeatEnable = false;
    public static long repeatTime;

    public static boolean oftenEnable = false;
    public static long oftenTime;
    public static int oftenNumber;

    public static boolean cmdEnable = false;
    public static long cmdTime;
    public static int cmdNumber;

    public static boolean logEnable = false;
    public static boolean logClear = false;
    public static long logTime;
    public static int logCheck;
    public static boolean logItem = false;
    public static boolean logDense = false;
    public static boolean logTrash = false;
    public static boolean logGather = false;
    public static boolean logMobs = false;
    public static boolean logChat = false;

    public static boolean dreamBind = false;
    public static boolean citizens = false;
    public static boolean mythicMobs = false;
    public static boolean quickShop = false;
    public static boolean pixelmon = false;
    public static MobManager mobManager;
    public static BukkitAPIHelper apiHelper;

    public static boolean uranium = false;
    public static File itemFile;
    public static File denseFile;
    public static File trashFile;
    public static File gatherFile;
    public static File mobsFile;
    public static File chatFile;

    public static HashMap<String,String> lang;
    public static Timer timer = new Timer();
    public static boolean offhand = false;
    public static String country;
    public static boolean bStats = true;
    public static MobsManager mobsManager;

    @Override
    public void onEnable() {
        // Plugin startup logic
        pl = this;
        try {
            EquipmentSlot.OFF_HAND.getClass();
            offhand = true;
        }catch (Throwable ignored){

        }
        country = "CN";
        if (System.getProperties().contains("user.country")){
            country =  System.getProperty("user.country");
        }
        console("&a插件交流群:&b 362221212");
        if (bStats){
            Metrics metrics = new Metrics(this, 14219);
            metrics.addCustomChart(new Metrics.SimplePie("chart_id", () -> "My value"));
        }
        getServer().getPluginManager().registerEvents(new RightEvent(),this);
        getServer().getPluginManager().registerEvents(new TrashEvent(),this);
        getServer().getPluginManager().registerEvents(new ChatEvent(),this);
        try {
            Class.forName("org.bukkit.event.entity.EntitySpawnEvent");
            getServer().getPluginManager().registerEvents(new Event(),this);
        } catch (ClassNotFoundException ignored) {
            console("&a检测到&b Uranium端 &a,开启兼容！");
            uranium = true;
            getServer().getPluginManager().registerEvents(new Event.Spawn(),this);
        }
        new ACommand();
        Objects.requireNonNull(getCommand("DreamCleaner")).setExecutor(new Cmd());
        timer.schedule(new ClearTask(),0,1000);
        timer.schedule(new ChatTask(),0,1000);
        new LogTask().runTaskTimerAsynchronously(this,0,1200);
        new BukkitRunnable(){
            @Override
            public void run() {
                if (getServer().getPluginManager().getPlugin("DreamBind") != null){
                    dreamBind = true;
                    console("&a成功兼容前置插件 &6DreamBind");
                }
                if (getServer().getPluginManager().getPlugin("Citizens") != null){
                    citizens = true;
                    console("&a成功兼容前置插件 &bCitizens");
                }
                if (getServer().getPluginManager().getPlugin("MythicMobs") != null){
                    mythicMobs = true;
                    try {
                        mobManager = new MobManager(MythicMobs.inst());
                        mobsManager = MobsManager.A;
                        console("&a成功兼容前置插件 &cMythicMobs");
                    }catch (Throwable throwable){
                        apiHelper = MythicBukkit.inst().getAPIHelper();
                        mobsManager = MobsManager.B;
                        console("&a成功兼容前置插件 &cMythicMobs5.0");
                    }
                }
                if (getServer().getPluginManager().getPlugin("QuickShop") != null){
                    quickShop = true;
                    ShopManager.checkShop();
                    console("&a成功兼容前置插件 &eQuickShop");
                }
                try {
                    Class.forName("com.pixelmonmod.pixelmon.entities.pixelmon.EntityPixelmon");
                    console("&a成功兼容前置MOD &ePixelmon");
                    Class.forName("me.dz.mod.dreamcleaner.entity.EntityManager");
                    try {
                        Class.forName("catserver.api.bukkit.event.ForgeEvent");
                        console("&a成功检测到CatServer可以开启对宝可梦的支持");
                        pixelmon = true;
                        getServer().getPluginManager().registerEvents(new PokeEvent.CatServer(),pl);
                    }catch (ClassNotFoundException ignored){
                        try {
                            Class.forName("com.mohistmc.api.event.BukkitHookForgeEvent");
                            console("&a成功检测到Mohist可以开启对宝可梦的支持");
                            pixelmon = true;
                            getServer().getPluginManager().registerEvents(new PokeEvent.Mohist(),pl);
                        }catch (ClassNotFoundException ignored1){
                            console("&a未检测到可以兼容的MOD服务端核心,无法开启对宝可梦的支持");
                        }
                    }
                } catch (ClassNotFoundException ignored) {
                    console("&c未检测到&b DreamCleanerMod &c无法进行对宝可梦的兼容");
                }
                loadConfig();
            }
        }.runTaskLater(this,1);
    }

    @Override
    public void onDisable() {
        // Plugin shutdown logic
        if (timer != null)timer.cancel();
        for (Player player : TrashUtils.open){
            player.closeInventory();
        }
    }

    public void loadConfig(){
        File config = new File(getDataFolder(),"config.yml");
        if (!config.exists())saveResourceGeneration("config",config);
        YamlConfiguration c = YamlConfiguration.loadConfiguration(config);

        bStats = c.getBoolean("bStats",true);

        prefix = format(c.getString("prefix"));
        itemEnable = c.getBoolean("waste.enable");
        ConfigurationSection waste = c.getConfigurationSection("waste");
        assert waste != null;
        itemAsync = waste.getBoolean("async");
        itemTime = waste.getInt("time");
        String remindMsg = waste.getString("remind");
        itemAlone = waste.getBoolean("alone");
        itemIssue = waste.getBoolean("issue");
        itemCheck = waste.getBoolean("check");
        itemRemind = new ArrayList<>();
        assert remindMsg != null;
        for (String msg : remindMsg.split(",")){
            try {
                int i = Integer.parseInt(msg);
                itemRemind.add(i);
            }catch (NumberFormatException ignored){

            }
        }
        itemBlackWorld = new ArrayList<>(waste.getStringList("world"));
        itemType = new ArrayList<>();
        waste.getStringList("type").forEach(msg -> itemType.add(msg.toUpperCase()));
        itemVagueType = new ArrayList<>();
        waste.getStringList("vaguetype").forEach(msg -> itemVagueType.add(msg.toUpperCase()));
        itemName = new ArrayList<>();
        waste.getStringList("name").forEach(msg -> itemName.add(format(msg)));
        itemVagueName = new ArrayList<>();
        waste.getStringList("vaguename").forEach(msg -> itemVagueName.add(format(msg)));
        itemLore = new ArrayList<>();
        waste.getStringList("lore").forEach(msg -> itemLore.add(format(msg)));
        itemVagueLore = new ArrayList<>();
        waste.getStringList("vaguelore").forEach(msg -> itemVagueLore.add(format(msg)));
        itemCleaner = new ArrayList<>();
        ConfigurationSection more = waste.getConfigurationSection("more");
        assert more != null;
        for (String name : more.getKeys(false)){
            if (more.getBoolean(name)){
                try {
                    ItemType type = ItemType.valueOf(name.toUpperCase());
                    if (type.canRun()){
                        itemCleaner.add(type);
                    }
                }catch (Exception ignored) {

                }
            }
        }
        itemClearMsg = new HashMap<>();
        ConfigurationSection message = waste.getConfigurationSection("message");
        assert message != null;
        itemMsg = message.getBoolean("enable",true);
        for (String name : message.getKeys(false)){
            itemClearMsg.put(name,format(Objects.requireNonNull(message.getString(name)).replace("<prefix>",prefix)));
        }
        alias = new HashMap<>();
        for (String name : Objects.requireNonNull(c.getConfigurationSection("alias")).getKeys(false)){
            alias.put(name,format(c.getString("alias." + name)));
        }
        itemBar = waste.getBoolean("bossbar.enable");
        if (itemBar){
            try {
                itemBossBar = Bukkit.createBossBar(itemBarMsg,BarColor.RED, BarStyle.SOLID);
                itemBarMsg = format(Objects.requireNonNull(waste.getString("bossbar.message")).replace("<prefix>",prefix));
                itemBarTime = waste.getInt("bossbar.time");
            }catch (NoClassDefFoundError ignored){
                itemBar = false;
                console("&c当前版本不支持BossBar");
            }
        }
        itemAction = waste.getBoolean("actionbar.enable");
        if (itemAction){
            try {
                ChatMessageType actionBar = ChatMessageType.ACTION_BAR;
                itemActionMsg = format(Objects.requireNonNull(waste.getString("actionbar.message")).replace("<prefix>",prefix));
                itemBarTime = waste.getInt("actionbar.time");
            }catch (NoClassDefFoundError ignored){
                itemAction = false;
                console("&c当前版本不支持ActionBar");
            }
        }

        denseEnable = c.getBoolean("dense.enable");
        ConfigurationSection dense = c.getConfigurationSection("dense");
        assert dense != null;
        denseAsync = dense.getBoolean("async");
        denseScope = dense.getDouble("scope");
        denseAmount = dense.getInt("amount");
        denseRemind = dense.getInt("remind");
        denseCheck = dense.getBoolean("check");
        denseBlackWorld = new ArrayList<>(dense.getStringList("world"));
        denseType = new ArrayList<>();
        dense.getStringList("type").forEach(msg -> denseType.add(msg.toUpperCase()));
        denseVagueType = new ArrayList<>();
        dense.getStringList("vaguetype").forEach(msg -> denseVagueType.add(msg.toUpperCase()));
        denseName = new ArrayList<>();
        dense.getStringList("name").forEach(msg -> denseName.add(format(msg)));
        denseVagueName = new ArrayList<>();
        dense.getStringList("vaguename").forEach(msg -> denseVagueName.add(format(msg)));
        denseLore = new ArrayList<>();
        dense.getStringList("lore").forEach(msg -> denseLore.add(format(msg)));
        denseVagueLore = new ArrayList<>();
        dense.getStringList("vaguelore").forEach(msg -> denseVagueLore.add(format(msg)));
        denseCleaner = new ArrayList<>();
        more = dense.getConfigurationSection("item");
        assert more != null;
        for (String name : more.getKeys(false)){
            if (more.getBoolean(name)){
                try {
                    ItemType type = ItemType.valueOf(name.toUpperCase());
                    if (type.canRun()){
                        if (type == ItemType.ITEM){
                            denseCleaner.add(ItemType.DENSEITEM);
                        }else {
                            denseCleaner.add(type);
                        }
                    }
                }catch (Exception ignored) {

                }
            }
        }
        denseMsg = format(dense.getString("message"));

        trashEnable = c.getBoolean("trash.enable");
        ConfigurationSection trash = c.getConfigurationSection("trash");
        assert trash != null;
        trashSize = trash.getInt("size");
        trashReset = trash.getInt("reset");
        trashPermission = trash.getString("permission");
        trashTitle = format(trash.getString("title"));
        trashItem = trash.getBoolean("item");
        trashType = new ArrayList<>();
        trash.getStringList("type").forEach(msg -> trashType.add(msg.toUpperCase()));
        trashVagueType = new ArrayList<>();
        trash.getStringList("vaguetype").forEach(msg -> trashVagueType.add(msg.toUpperCase()));
        trashName = new ArrayList<>();
        trash.getStringList("name").forEach(msg -> trashName.add(format(msg)));
        trashVagueName = new ArrayList<>();
        trash.getStringList("vaguename").forEach(msg -> trashVagueName.add(format(msg)));
        trashLore = new ArrayList<>();
        trash.getStringList("lore").forEach(msg -> trashLore.add(format(msg)));
        trashVagueLore = new ArrayList<>();
        trash.getStringList("vaguelore").forEach(msg -> trashVagueLore.add(format(msg)));
        trashOnName = format(trash.getString("up..name"));
        trashOnType = Objects.requireNonNull(trash.getString("up.type")).toUpperCase();
        trashOnLore = new ArrayList<>();
        trash.getStringList("up.lore").forEach(msg -> trashOnLore.add(format(msg)));
        trashDownName = format(trash.getString("down.name"));
        trashDownType = Objects.requireNonNull(trash.getString("down.type")).toUpperCase();
        trashDownLore = new ArrayList<>();
        trash.getStringList("down.lore").forEach(msg -> trashDownLore.add(format(msg)));
        message = trash.getConfigurationSection("message");
        trashMsg = new HashMap<>();
        assert message != null;
        for (String msg : message.getKeys(false)){
            trashMsg.put(msg,format(message.getString(msg)));
        }

        File mobsFile = new File(getDataFolder(),"mobs.yml");
        if (!mobsFile.exists())saveResourceGeneration("mobs",mobsFile);
        YamlConfiguration mc = YamlConfiguration.loadConfiguration(mobsFile);

        gatherEnable = mc.getBoolean("gather.enable");
        ConfigurationSection gather = mc.getConfigurationSection("gather");
        assert gather != null;
        gatherAsync = gather.getBoolean("async");
        gatherScope = gather.getDouble("scope");
        gatherAmount = gather.getInt("amount");
        gatherRemind = gather.getInt("remind");
        gatherCheck = gather.getBoolean("check");
        gatherMythic = gather.getBoolean("mythic");
        gatherBlackWorld = new ArrayList<>(gather.getStringList("world"));
        gatherCheckName = gather.getBoolean("name.enable");
        gatherTameable = gather.getBoolean("tameable");
        if (gatherTameable){
            try {
                Object object = Tameable.class;
            }catch (NoClassDefFoundError ignored){
                gatherTameable = false;
                console("&c该服务端没有可驯化生物类");
            }
        }
        gatherName = new ArrayList<>();
        gather.getStringList("name.name").forEach(msg -> gatherName.add(format(msg)));
        gatherVagueName = new ArrayList<>();
        gather.getStringList("name.vaguename").forEach(msg -> gatherVagueName.add(format(msg)));
        gatherType = new ArrayList<>();
        gather.getStringList("type").forEach(msg -> gatherType.add(msg.toUpperCase()));
        gatherVagueType = new ArrayList<>();
        gather.getStringList("vaguetype").forEach(msg -> gatherVagueType.add(msg.toUpperCase()));
        gatherEntity = new ArrayList<>();
        gather.getStringList("entity").forEach(msg -> gatherEntity.add(msg.toUpperCase()));
        gatherVagueEntity = new ArrayList<>();
        gather.getStringList("vagueentity").forEach(msg -> gatherVagueEntity.add(msg.toUpperCase()));
        gatherMsg = format(gather.getString("message"));
        gatherCleaner = new ArrayList<>();
        more = gather.getConfigurationSection("mobs");
        assert more != null;
        for (String name : more.getKeys(false)){
            if (more.getBoolean(name)){
                try {
                    MobsType type = MobsType.valueOf(name.toUpperCase());
                    if (type.canRun()){
                        gatherCleaner.add(type);
                    }
                }catch (Exception ignored) {

                }
            }
        }

        mobsEnable = mc.getBoolean("mobs.enable");
        ConfigurationSection mobs = mc.getConfigurationSection("mobs");
        assert mobs != null;
        mobsAsync = mobs.getBoolean("async");
        mobsTime = mobs.getInt("time");
        remindMsg = mobs.getString("remind");
        mobsAlone = mobs.getBoolean("alone");
        mobsIssue = mobs.getBoolean("issue");
        mobsCheck = mobs.getBoolean("check");
        mobsMeet = mobs.getBoolean("meet");
        mobsServer = mobs.getBoolean("server");
        mobsAmount = Objects.requireNonNull(mobs.getString("amount")).replace(" ","");
        mobsMythic = mobs.getBoolean("mythic");
        mobsRemind = new ArrayList<>();
        assert remindMsg != null;
        for (String msg : remindMsg.split(",")){
            try {
                int i = Integer.parseInt(msg);
                mobsRemind.add(i);
            }catch (NumberFormatException ignored){

            }
        }
        mobsBlackWorld = new ArrayList<>(mobs.getStringList("world"));
        mobsTameable = mobs.getBoolean("tameable");
        if (mobsTameable){
            try {
                Object object = Tameable.class;
            }catch (NoClassDefFoundError ignored){
                mobsTameable = false;
                console("&c该服务端没有可驯化生物类");
            }
        }
        mobsCheckName = mobs.getBoolean("name.enable");
        mobsName = new ArrayList<>();
        mobs.getStringList("name.name").forEach(msg -> mobsName.add(format(msg)));
        mobsVagueName = new ArrayList<>();
        mobs.getStringList("name.vaguename").forEach(msg -> mobsVagueName.add(format(msg)));
        mobsType = new ArrayList<>();
        mobs.getStringList("type").forEach(msg -> mobsType.add(msg.toUpperCase()));
        mobsVagueType = new ArrayList<>();
        mobs.getStringList("vaguetype").forEach(msg -> mobsVagueType.add(msg.toUpperCase()));
        mobsEntity = new ArrayList<>();
        mobs.getStringList("entity").forEach(msg -> mobsEntity.add(msg.toUpperCase()));
        mobsVagueEntity = new ArrayList<>();
        mobs.getStringList("vagueentity").forEach(msg -> mobsVagueEntity.add(msg.toUpperCase()));
        mobsCleaner = new ArrayList<>();
        more = mobs.getConfigurationSection("mobs");
        assert more != null;
        for (String name : more.getKeys(false)){
            if (more.getBoolean(name)){
                try {
                    MobsType type = MobsType.valueOf(name.toUpperCase());
                    if (type.canRun()){
                        mobsCleaner.add(type);
                    }
                }catch (Exception ignored) {

                }
            }
        }
        mobsClearMsg = new HashMap<>();
        message = mobs.getConfigurationSection("message");
        assert message != null;
        mobsMsg = message.getBoolean("enable",true);
        for (String name : message.getKeys(false)){
            mobsClearMsg.put(name,format(Objects.requireNonNull(message.getString(name)).replace("<prefix>",prefix)));
        }
        mobsBar = mobs.getBoolean("bossbar.enable");
        if (mobsBar){
            try {
                mobsBarMsg = format(Objects.requireNonNull(mobs.getString("bossbar.message")).replace("<prefix>",prefix));
                mobsBarTime = mobs.getInt("bossbar.time");
                mobsBossBar = Bukkit.createBossBar(mobsBarMsg,BarColor.RED, BarStyle.SOLID);
            }catch (NoClassDefFoundError ignored){
                mobsBar = false;
                console("&c当前版本不支持BossBar");
            }
        }
        mobsAction = mobs.getBoolean("actionbar.enable");
        if (mobsAction){
            try {
                ChatMessageType actionBar = ChatMessageType.ACTION_BAR;
                mobsActionMsg = format(Objects.requireNonNull(mobs.getString("actionbar.message")).replace("<prefix>",prefix));
                mobsBarTime = mobs.getInt("actionbar.time");
            }catch (NoClassDefFoundError ignored){
                mobsAction = false;
                console("&c当前版本不支持ActionBar");
            }
        }
        ConfigurationSection poke = mobs.getConfigurationSection("poke");
        assert poke != null;
        pokeEnable = poke.getBoolean("enable");
        if (!pixelmon){
            pokeEnable = false;
        }
        pokeShiny = poke.getBoolean("shiny");
        pokeLike = poke.getLong("live");
        pokeClear = new ArrayList<>(poke.getStringList("unClear"));
        if (pokeEnable){
            PokeEvent.pokes.clear();
            pokeTypes = new ArrayList<>();
            for (String name : Objects.requireNonNull(poke.getConfigurationSection("type")).getKeys(false)){
                if (poke.getBoolean("type." + name)){
                    try {
                        PokeType type = PokeType.valueOf(name.toUpperCase());
                        pokeTypes.add(type);
                    }catch (Exception ignored){

                    }
                }
            }
            for (World world : Bukkit.getWorlds()){
                if (WorldManager.canClear(world,mobsBlackWorld,mobsCheck)){
                    ArrayList<Entity> entities = EntityManager.getEntity(world.getName());
                    for (Entity entity : entities){
                        if (entity instanceof EntityPixelmon){
                            PokeEvent.canClear((EntityPixelmon) entity);
                        }
                    }
                }
            }
        }

        File msgFile = new File(getDataFolder(),"message.yml");
        if (!msgFile.exists())saveResourceGeneration("message",msgFile);
        YamlConfiguration msg = YamlConfiguration.loadConfiguration(msgFile);

        lang = new HashMap<>();
        for (String name : msg.getKeys(false)){
            lang.put(name.toLowerCase(),format(msg.getString(name)));
        }

        File chat = new File(getDataFolder(),"chat.yml");
        if (!chat.exists())saveResourceGeneration("chat",chat);
        YamlConfiguration cc = YamlConfiguration.loadConfiguration(chat);

        ConfigurationSection cache = cc.getConfigurationSection("cache");
        assert cache != null;
        cacheEnable = cache.getBoolean("enable");
        cacheReset = cache.getInt("reset");
        ConfigurationSection repeat = cc.getConfigurationSection("repeat");
        assert repeat != null;
        repeatEnable = repeat.getBoolean("enable");
        repeatTime = repeat.getLong("time");
        ConfigurationSection often = cc.getConfigurationSection("often");
        assert often != null;
        oftenEnable = often.getBoolean("enable");
        oftenTime = often.getLong("time");
        oftenNumber = often.getInt("number");
        ConfigurationSection cmd = cc.getConfigurationSection("cmd");
        assert cmd != null;
        cmdEnable = cmd.getBoolean("enable");
        cmdTime = cmd.getLong("time");
        cmdNumber = cmd.getInt("number");

        File log = new File(getDataFolder(),"log.yml");
        if (!log.exists())saveResourceGeneration("log",log);
        YamlConfiguration lc = YamlConfiguration.loadConfiguration(log);

        logEnable = lc.getBoolean("enable");
        if (logEnable){
            File logs = new File(getDataFolder(),"log");
            if (!logs.exists())logs.mkdirs();
            itemFile = new File(logs,"item");
            if (!itemFile.exists())itemFile.mkdirs();
            denseFile = new File(logs,"dense");
            if (!denseFile.exists())denseFile.mkdirs();
            trashFile = new File(logs,"trash");
            if (!trashFile.exists())trashFile.mkdirs();
            gatherFile = new File(logs,"gather");
            if (!gatherFile.exists())gatherFile.mkdirs();
            chatFile = new File(logs,"chat");
            if (!chatFile.exists())chatFile.mkdirs();
            DreamCleaner.mobsFile = new File(logs,"mobs");
            if (!DreamCleaner.mobsFile.exists())DreamCleaner.mobsFile.mkdirs();
            logClear = lc.getBoolean("auto.enable");
            if (logClear){
                logTime = lc.getLong("auto.time") * 24 * 60 * 60 * 1000;
                logCheck = lc.getInt("auto.check");
            }
            ConfigurationSection record = lc.getConfigurationSection("record");
            assert record != null;
            logItem = record.getBoolean("item");
            logDense = record.getBoolean("dense");
            logTrash = record.getBoolean("trash");
            logGather = record.getBoolean("gather");
            logMobs = record.getBoolean("mobs");
            logChat = record.getBoolean("chat");
        }

        if (!country.equals("CN")){
            File lang = new File(getDataFolder(),"lang");
            if (lang.exists()){
                lang.delete();
            }
        }

        TrashUtils.reset(true);
    }

    public void saveResourceGeneration(String config,File file){
        if (country.equals("CN")){
            saveResource(config + ".yml",true);
        }else {
            File en = new File(getDataFolder(),"lang\\" + config + "-en.yml");
            saveResource("lang\\" + config + "-en.yml",true);
            en.renameTo(file);
        }
    }
}
